package util

import (
	"fmt"
	"time"
)

// MsTimeDiff 毫秒时间差
func MsTimeDiff(before, after time.Time) (res uint64) {
	beforeTime, _ := time.ParseInLocation("2006-01-02 15:04:05.000", before.Format("2006-01-02 15:04:05.000"), time.Local)
	afterTime, _ := time.ParseInLocation("2006-01-02 15:04:05.000", after.Format("2006-01-02 15:04:05.000"), time.Local)
	beforeMs := beforeTime.UnixMilli()
	afterMs := afterTime.UnixMilli()
	if afterMs > beforeMs {
		res = uint64(afterMs - beforeMs)
	}
	return
}

// 获取毫秒耗时描述
func LatencyDescByMs(msTime uint64) string {
	if msTime < 1000 {
		return fmt.Sprintf("%d毫秒", msTime)
	}
	if msTime < 60000 {
		sNum := msTime / 1000
		msNum := msTime % 1000
		return fmt.Sprintf("%d秒%d毫秒", sNum, msNum)
	}
	if msTime < 3600000 {
		mNum := msTime / 60000
		sNum := (msTime % 60000) / 1000
		return fmt.Sprintf("%d分%d秒", mNum, sNum)
	}
	if msTime < 86400000 {
		hNum := msTime / 3600000
		mNum := (msTime % 3600000) / 60000
		return fmt.Sprintf("%d小时%d分", hNum, mNum)
	}
	dNum := msTime / 86400000
	hNum := (msTime % 86400000) / 3600000
	return fmt.Sprintf("%d天%d小时", dNum, hNum)
}
